package flatlandRPGTest;

import flatlandRPG.Entity;
import java.util.ArrayList;
import flatlandRPG.Actor;
import flatlandRPG.GameText;
import flatlandRPG.TeleportTrigger;
import flatlandRPG.Building;
import flatlandRPG.Map;
import flatlandRPG.Player;
import flatlandRPG.TestableKeyboard;
import org.lwjgl.input.Keyboard;
import org.lwjgl.util.vector.Vector2f;
import student.TestCase;

// -------------------------------------------------------------------------
/**
 * Tests that the map class works correctly.
 *
 * @author evant
 * @version Apr 20, 2011
 */
public class MapTest
    extends TestCase
{
    private Map    testMap;
    private Player player;


    public void setUp()
    {
        testMap = new Map();
        player = new Player();
    }


    // ----------------------------------------------------------
    /**
     * Tests that the map calls doLogic on everything that has logic.
     */
    public void testMapDoLogic()
    {
        testMap.add(player);
        testMap.add(new Building(new Vector2f(0.1f, 0.1f), new Vector2f(
            0.1f,
            0.2f), new Vector2f(0.2f, 0.2f), new Vector2f(0.2f, 0.1f)));

        TestableKeyboard.setKeyDown(Keyboard.KEY_DOWN);
        for (int i = 0; i < 10; i++)
        {
            testMap.doLogic();
        }

        assertEquals(0, player.getX(), 0.0001f);
        assertEquals(-Player.SPEED * 10, player.getY(), 0.0001f);
    }


    /**
     * Tests that things are added correctly to the map.
     */
    public void testAdd()
    {
        testMap.add(player);
        testMap.add(new Building(
            new Vector2f(0, 0),
            new Vector2f(1, 1),
            new Vector2f(0, 1)));
        testMap.add(new TeleportTrigger(
            player,
            new Vector2f(0, 0),
            new Vector2f(1, 1),
            0.5f));
        testMap.add(new Actor(0.5f, 4, new GameText(100), "Test text"));

        assertEquals(4, testMap.getEntities().size());
        assertTrue(testMap.getEntities().get(0) instanceof Player);
        assertTrue(testMap.getEntities().get(1) instanceof Building);
        assertTrue(testMap.getEntities().get(2) instanceof TeleportTrigger);
        assertTrue(testMap.getEntities().get(3) instanceof Actor);
    }

    // ----------------------------------------------------------
    /**
     * Tests that adding a collection of entities works correctly.
     */
    public void testListAdd() {
        ArrayList<Entity> list = new ArrayList<Entity>();

        list.add(player);
        list.add(new Building(
            new Vector2f(0, 0),
            new Vector2f(1, 1),
            new Vector2f(0, 1)));
        list.add(new TeleportTrigger(
            player,
            new Vector2f(0, 0),
            new Vector2f(1, 1),
            0.5f));
        list.add(new Actor(0.5f, 4, new GameText(100), "Test text"));

        testMap.add(list);

        assertEquals(4, testMap.getEntities().size());
        assertTrue(testMap.getEntities().get(0) instanceof Player);
        assertTrue(testMap.getEntities().get(1) instanceof Building);
        assertTrue(testMap.getEntities().get(2) instanceof TeleportTrigger);
        assertTrue(testMap.getEntities().get(3) instanceof Actor);
    }

    // ----------------------------------------------------------
    /**
     * Tests that getPlayer returns the last player added to the map.
     */
    public void testGetPlayer() {
        testMap.add(new Player());
        testMap.add(player);

        assertEquals(player, testMap.getPlayer());
    }
}
